package com.example.mydianping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.mydianping.dto.Result;
import com.example.mydianping.mapper.ShopTypeMapper;
import com.example.mydianping.pojo.ShopType;
import com.example.mydianping.service.ShopTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ShopTypeServiceImpl implements ShopTypeService {
    @Autowired
    private ShopTypeMapper shopTypeMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result getList() {
        //先从 redis 中查询
        String listJson = stringRedisTemplate.opsForValue().get("shop-type:list:");
        if(listJson != null){
            return Result.ok(JSONUtil.toList(listJson, ShopType.class));
        }
        //redis 中没有,在 mysql 中查询
        List<ShopType> shopTypes = shopTypeMapper.selectAll();
        if(shopTypes == null)return Result.fail("查询出现错误");
        stringRedisTemplate.opsForValue().set("shop-type:list:", JSONUtil.toJsonStr(shopTypes));
        return Result.ok(shopTypes);
    }
}
